<?php
	require_once ("./General/validate.php");
	require_once ("./View/RegisterView.php");
	require_once ("./View/LoginView.php");
	require_once ("./Model/RegisterHandler.php");
	
	
	define('error', 'Please check your information you entered');

class RegisterController {
	   	private $m_user = 'username';
		
		
	public function DoControll(){
		$page = new Page();
		$rw = new RegisterView();
		$lw = new LoginView();
		$rc = new RegisterController();	
		$page->m_title = "Labb3";
		$page->m_body = $rw->BannerBox() . $rw->RegisterForm();
		

		if($rw->TriedToRegister()){
			if($rc->Register() == false){
			$page->m_body = $rw->BannerBox() . error . $rw->RegisterForm();
			}
			if($rc->Register() == true){
			$page->m_body = $lw->BannerBox() . $lw->DoLogoutBox() . $lw->DeleteAccountLink();
			}			
		}
		if($rw->TriedToAbort()){
			$page->m_body = $lw->BannerBox() . $lw->DoLoginBox();		
		}
			
		
		
		return $page;
		
		
		
	}
	
	public function Register(){
		$rw = new RegisterView();
		$db = new RegisterHandler();
		
		if($this->ValidateEmail() == true){
			if($this->ValidatePassword() == true){
				if ($this->ValidateUsername() == true ) {
					if ($this->CheckPassword() == true ) {
						if ($db->CheckIfRegistered($rw->GetUsername()) == false) {
							$db->RegisterToDB($rw->GetUsername(), $rw->GetPassword());
							$lh = new LoginHandler();
							if($lh->DoLoginDB($rw->GetUsername(), $rw->GetPassword()) == true){	
							return true;	
							}
						}
						return false;
					}
					return false;
				}
				return false;
			}
			return false;
		}

		return false;
	}
	public function CheckPassword(){
		$rw = new RegisterView();	
		if($rw->GetPassword() == $rw->GetPassword2()){
			return true;
		}
		return false;
	}
	public function ValidateUsername(){
		$rw = new RegisterView();	
		$test = new validate();
		if($test->username($rw->GetUsername()) == true){
			return true;
		}
		return false;
	}
	public function ValidateEmail(){
		$rw = new RegisterView();	
		$test = new validate();
		if($test->email($rw->GetEmail()) == true){
			return true;
		}
		return false;
	}
	public function ValidatePassword(){
		$rw = new RegisterView();	
		$test = new validate();
		if($test->password($rw->GetPassword()) == true){
			return true;
		}
		return false;
	}
	
}
